import { NextResponse } from "next/server";
import permissionModule from "@/modules/permission";

export async function GET(request) {
  const codeMap = { 0: '查询全部权限成功', 1: '查询权限失败' }
  const id = request.nextUrl.searchParams.get('id');
  if (id) {
    const data = await permissionModule.getById(Number(id));
    if (!data) {
      return NextResponse.json({ code: 1, codeMap, message: "Permission not found", data: null }, { status: 404 })
    } else {
      return NextResponse.json({ code: 0, codeMap, message: "Success", data: data }, { status: 200 })
    }
  }
  const data = await permissionModule.getAll();
  if (!data) {
    return NextResponse.json({ code: 1, codeMap, message: "Permission not found", data: null }, { status: 404 })
  } else {
    return NextResponse.json({ code: 0, codeMap, message: "Success", data: data }, { status: 200 })
  }
}